<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/7
 * Time: 13:59
 */

namespace Common\Log;

class TypeLog {

    const LOG_TYPE_ARTICLE_BROWSING             = 1; // 浏览文章
    const LOG_TYPE_ARTICLE_LIKE                 = 2; // 点赞文章
    const LOG_TYPE_ARTICLE_COLLECT              = 3; // 收藏文章 or 问题回答
    const LOG_TYPE_QUESTION                     = 4; // 浏览问答
    const LOG_TYPE_LIKE_ANSWER                  = 5; // 点赞问题答案
    const LOG_TYPE_DISLIKE_ANSWER               = 6; // 反对问题答案
    const LOG_TYPE_EXCHANGE_ACT_TICKET          = 7; // 兑换活动票种
    const LOG_TYPE_ORDER_REFUND                 = 8; // 订单退款
    const LOG_TYPE_ORDER_REFUND_REFUSE          = 9; // 拒绝退款


    private $user_id            = 0;
    private $from_user_id       = 0;
    private $type               = 0;
    private $item_id            = 0;

    // 添加通用的log
    public static function addNormalLog($user_id, $from_user_id, $type, $item_id)
    {
        $log = new self();
        $log->setLogAttr('user_id', $user_id);
        $log->setLogAttr('from_user_id', $from_user_id);
        $log->setLogAttr('type', $type);
        $log->setLogAttr('item_id', $item_id);
        $log->AddTypeLog();
    }

    // 浏览
    public static function AddBrowsingLog($user_id, $from_user_id, $post_id)
    {

        $model  = M('type_log');
        $log    = $model->where(['user_id' => $user_id, 'type' => self::LOG_TYPE_ARTICLE_BROWSING, 'item_id' => $post_id])->find();
        if ($log) {
            $model->where(['id' => $log['id']])->save(['time' => time()]);
            return true;
        }

        $log = new self();
        $log->setLogAttr('user_id', $user_id);
        $log->setLogAttr('from_user_id', $from_user_id);
        $log->setLogAttr('type', self::LOG_TYPE_ARTICLE_BROWSING);
        $log->setLogAttr('item_id', $post_id);

        $log->AddTypeLog();
    }

    // 收藏
    public static function AddCollectLog($user_id, $from_user_id, $post_id)
    {
        $model  = M('type_log');
        $log    = $model->where(['user_id' => $user_id, 'type' => self::LOG_TYPE_ARTICLE_COLLECT, 'item_id' => $post_id])->find();
        if ($log) {
            $return['status']   = -1;
            $return['msg']      = "您已经收藏过啦";
            return $return;
        }

        $log = new self();
        $log->setLogAttr('user_id', $user_id);
        $log->setLogAttr('from_user_id', $from_user_id);
        $log->setLogAttr('type', self::LOG_TYPE_ARTICLE_COLLECT);
        $log->setLogAttr('item_id', $post_id);

        $re = $log->AddTypeLog();

        if ($re['status'] == 1) {
            $return['status']   = 1;
            $return['msg']      = "收藏成功";
            return $return;
        } else {
            $return['status']   = -2;
            $return['msg']      = "收藏失败，请稍后再试";
            return $return;
        }
    }


    // 点赞
    public static function AddLikeLog($user_id, $from_user_id, $post_id)
    {
        $model  = M('type_log');
        $log    = $model->where(['user_id' => $user_id, 'type' => self::LOG_TYPE_ARTICLE_LIKE, 'item_id' => $post_id])->find();
        if ($log) {
            $return['status']   = -1;
            $return['msg']      = "您已经赞过啦";
            return $return;
        }

        $log = new self();
        $log->setLogAttr('user_id', $user_id);
        $log->setLogAttr('from_user_id', $from_user_id);
        $log->setLogAttr('type', self::LOG_TYPE_ARTICLE_LIKE);
        $log->setLogAttr('item_id', $post_id);

        $re = $log->AddTypeLog();

        if ($re['status'] == 1) {
            $return['status']   = 1;
            $return['msg']      = "点赞成功";
            return $return;
        } else {
            $return['status']   = -2;
            $return['msg']      = "点赞失败，请稍后再试";
            return $return;
        }
    }


    // 问答浏览
    public static function AddQuestionLog($user_id, $from_user_id, $question_id)
    {

        $model  = M('type_log');
        $log    = $model->where(['user_id' => $user_id, 'type' => self::LOG_TYPE_QUESTION, 'item_id' => $question_id])->find();
        if ($log) {
            $model->where(['id' => $log['id']])->save(['time' => time()]);
            return true;
        }

        $log = new self();
        $log->setLogAttr('user_id', $user_id);
        $log->setLogAttr('from_user_id', $from_user_id);
        $log->setLogAttr('type', self::LOG_TYPE_QUESTION);
        $log->setLogAttr('item_id', $question_id);

        $log->AddTypeLog();
    }


    // 问答浏览
    public static function answerLikeLog($user_id, $answer_id)
    {
        $model  = M('type_log');
        $log    = $model->where(['user_id' => $user_id, 'type' => self::LOG_TYPE_LIKE_ANSWER, 'item_id' => $answer_id])->find();
        if ($log) {
            $model->where(['id' => $log['id']])->delete();
            $return['liked'] = 1;
        } else {
            $log = new self();
            $log->setLogAttr('user_id', $user_id);
            $log->setLogAttr('type', self::LOG_TYPE_LIKE_ANSWER);
            $log->setLogAttr('item_id', $answer_id);
            $log->AddTypeLog();
            $return['liked'] = 0;
        }
        return $return;
    }


    // 问答浏览
    public static function answerDislikeLog($user_id, $answer_id)
    {
        $model  = M('type_log');
        $log    = $model->where(['user_id' => $user_id, 'type' => self::LOG_TYPE_DISLIKE_ANSWER, 'item_id' => $answer_id])->find();
        if ($log) {
            $model->where(['id' => $log['id']])->delete();
            $return['disliked'] = 1;
        } else {
            $log = new self();
            $log->setLogAttr('user_id', $user_id);
            $log->setLogAttr('type', self::LOG_TYPE_DISLIKE_ANSWER);
            $log->setLogAttr('item_id', $answer_id);
            $log->AddTypeLog();
            $return['disliked'] = 0;
        }
        return $return;
    }


    // 新增typelog
    public function AddTypeLog()
    {
        $return = array('status' => 0);
        if (empty($this->user_id) || empty($this->type)) {
            $return['status']   = -1;
            $return['msg']      = "数据错误";
            return $return;
        }

        $data = array(
            'user_id'       => $this->user_id,
            'from_user_id'  => $this->from_user_id,
            'type'          => $this->type,
            'item_id'       => $this->item_id,
            'time'          => time(),
        );

        $re = M('type_log')->add($data);
        if ($re) {
            $return['status']   = 1;
            $return['msg']      = "操作成功";
            return $return;
        } else {
            $return['status']   = -2;
            $return['msg']      = "操作失败，请稍后再试";
            return $return;
        }
    }

    public function setLogAttr($col, $value)
    {
        if (!empty($value)) {
            $this->$col = $value;
        }
    }

    public function getLogAttr($col) {
        if (!empty($col)) {
            return $this->$col;
        }
    }

}